#include "smart_car_service_registry.h"

namespace smart_car
{
	smart_car_service_registry::smart_car_service_registry():
		logger("service_registry") {}

	bool smart_car_service_registry::add(smart_car_services service_type, service_interface* service)
	{
		service_map_t::iterator it = service_map.find(service_type);
		if ( it == service_map.end()) {
			service_map.insert(std::make_pair(service_type, service));
			logger.log_info("Added new service to registry [%d]", service_type);
			return true;
		}
		logger.log_info("Failed to add new service, service exists [%d]", service_type);
		return false;
	}

	void smart_car_service_registry::remove(smart_car_services service_type)
	{
		logger.log_info("Removing service [%d]", service_type);
		service_map.erase(service_type);
	}

	service_interface* smart_car_service_registry::find(smart_car_services service_type)
	{
		service_map_t::iterator it = service_map.find(service_type);
		if ( it != service_map.end()) {
			return it->second;
		}

		return NULL;
	}

	smart_car_service_registry::~smart_car_service_registry()
	{
		service_map_t::iterator it = service_map.begin();

		while ( it != service_map.end()) {
			delete it->second;
			service_map.erase(it);
			it = service_map.begin();
		}
	}
}